package svl;

import java.io.IOException;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Delegado.BD;
import dto.ClienteTO;

/**
 * Servlet implementation class ListarClientes
 */
@WebServlet("/ListarClientes")
public class ListarClientes extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ListarClientes() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    BD bd = new BD();
	    bd.LookupService(InetAddress.getLocalHost().getHostAddress());
	    List<ClienteTO> clis = bd.listarClientes();
	    List<String> razonSocialL = new ArrayList<String>();
	    List<String> cuilL= new ArrayList<String>();
	    List<String> calleL= new ArrayList<String>();
	    List<String> numeroL= new ArrayList<String>();
	    List<String> pisoL= new ArrayList<String>();
	    List<String> departamentoL= new ArrayList<String>();
	    List<String> localidadL= new ArrayList<String>();
	    List<String> areaL= new ArrayList<String>();
	    List<String> numerotelL= new ArrayList<String>();   
	    
	    for(ClienteTO c : clis){
	    	razonSocialL.add(c.getRazonSocial());
	    	cuilL.add(c.getCuil());
	    	calleL.add(c.getDomicilio().getCalle());
	    	numeroL.add(c.getDomicilio().getNumero());
	    	pisoL.add(c.getDomicilio().getPiso());
	    	departamentoL.add(c.getDomicilio().getDepartamento());
	    	localidadL.add(c.getDomicilio().getLocalidad());
	    	areaL.add(c.getTelefonos().get(0).getArea());
	    	numerotelL.add(c.getTelefonos().get(0).getNumero());
	    }
	    request.setAttribute("razonSocial", razonSocialL);
	    request.setAttribute("cuil", cuilL);
	    request.setAttribute("calle", calleL);
	    request.setAttribute("numero", numeroL);
	    request.setAttribute("piso", pisoL);
	    request.setAttribute("departamento", departamentoL);
	    request.setAttribute("localidad", localidadL);
	    request.setAttribute("area", areaL);
	    request.setAttribute("numeroTel", numerotelL);
	    request.setAttribute("lista", clis);
	    RequestDispatcher dispatcher = request.getRequestDispatcher("/jsp/ListarClientes.jsp");
	    dispatcher.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
